﻿Imports System.Data.SqlClient

Public Class BidangDokumenDb
    Dim connectionString As String

    Public Sub New(ByVal connectionString As String)
        Me.connectionString = connectionString
    End Sub

    Public Function isExist(ByVal bidangDokumen As String) As Boolean
        Dim total As Integer
        Dim connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand("select count(*) from TB_MST_BIDANG_DOK where bidang_dok = @bidangDokumen", connection)

        Try
            connection.Open()
            command.Parameters.AddWithValue("@bidangDokumen", bidangDokumen)
            total = CInt(command.ExecuteScalar())
        Catch ex As SqlException
            Throw ex
        Finally
            connection.Close()
        End Try

        If total > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function isExist(ByVal idBidangDok As String, ByVal bidangDokumen As String) As Boolean
        Dim total As Integer
        Dim connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand("select count(*) from TB_MST_BIDANG_DOK where id_bidang_dok = @idBidangDok and bidang_dok = @bidangDokumen", connection)

        Try
            connection.Open()
            command.Parameters.AddWithValue("@bidangDokumen", bidangDokumen)
            total = CInt(command.ExecuteScalar())
        Catch ex As SqlException
            Throw ex
        Finally
            connection.Close()
        End Try

        If total > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub insert(ByVal bidangDokumen As String, ByVal deskripsi As String, ByVal isActive As String)
        Dim connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand("insert into TB_MST_BIDANG_DOK (bidang_dok,deskripsi,is_active,tgl_update) values (@bidangDokumen, @deskripsi, @isActive, CURRENT_TIMESTAMP)", connection)
        Try
            connection.Open()
            command.Parameters.AddWithValue("@bidangDokumen", bidangDokumen)
            command.Parameters.AddWithValue("@deskripsi", deskripsi)
            command.Parameters.AddWithValue("@isActive", isActive)
            command.ExecuteNonQuery()
        Catch ex As SqlException
            Throw ex
        Finally
            connection.Close()
        End Try
    End Sub

    Public Sub update(ByVal id As String, ByVal bidangDokumen As String, ByVal deskripsi As String, ByVal isActive As String)
        Dim connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand("update TB_MST_BIDANG_DOK set bidang_dok=@bidangDokumen, deskripsi=@deskripsi, is_active=@isActive, tgl_update=current_timestamp where id_bidang_dok1d = @id", connection)
        Try
            connection.Open()
            command.Parameters.AddWithValue("@bidangDokumen", bidangDokumen)
            command.Parameters.AddWithValue("@deskripsi", deskripsi)
            command.Parameters.AddWithValue("@isActive", isActive)
            command.Parameters.AddWithValue("@id", id)
            command.ExecuteNonQuery()
        Catch ex As SqlException
            Throw ex
        Finally
            connection.Close()
        End Try
    End Sub
End Class
